using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class PHANQUYEN_QUANLY_DAL
    {
        private SqlConnection cn = null;
        public PHANQUYEN_QUANLY_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"]
                 .ConnectionString);
            cn.Open();
          
        }
        public void insert(PHANQUYEN_QUANLY_DTO pqqly)
        {
            SqlCommand cmd = new SqlCommand("PHANQUYEN_QUANLY_INSERT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TENQUYEN", pqqly.TenQuyen);
            cmd.ExecuteNonQuery();
        }
        public void update(PHANQUYEN_QUANLY_DTO pqqly)
        {
            SqlCommand cmd = new SqlCommand("PHANQUYEN_QUANLY_UPDATE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MAQUYEN", pqqly.MaQuyen);
            cmd.Parameters.AddWithValue("@TENQUYEN", pqqly.TenQuyen);
            cmd.ExecuteNonQuery();
        }
        public void delete(int mquyen)
        {
            SqlCommand cmd = new SqlCommand("PHANQUYEN_QUANLY_DELETE", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MAQUYEN", mquyen);
            cmd.ExecuteNonQuery();
        }
        public PhanQuyen_QuanLyColection Get(String where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("PHANQUYEN_QUANLY_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@WHERE", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PAGESIZE", pageSize);
            cm.Parameters.AddWithValue("@ORDERBY", orderBy);
            cm.Parameters.AddWithValue("@ORDERDIRECTION", orderDirection);

            //cm.Parameters .Add (new SqlParameter ("@TOTALRECORD",DbType.Int32)).Direction=ParameterDirection.Output 
            cm.Parameters.Add(new SqlParameter("@TOTALRECORDs", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            PhanQuyen_QuanLyColection pqqlCollection = new PhanQuyen_QuanLyColection();
            while (dr.Read())
            {
                PHANQUYEN_QUANLY_DTO pquyenql = new PHANQUYEN_QUANLY_DTO();
                pquyenql.MaQuyen = int.Parse(dr.GetValue(0).ToString());
                pquyenql.TenQuyen= dr.GetValue(1).ToString();
                pqqlCollection.Add(pquyenql);


            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TOTALRECORDs"].Value.ToString());
            return pqqlCollection;


        }
    }
}
